package ua.com.infinite.mobile.contentprovider;

import ua.com.infinite.mobile.db.DBAdapter;
import ua.com.infinite.mobile.db.TableNews;
import ua.com.infinite.mobile.db.TableTransaktions;
import ua.com.infinite.mobile.db.TableUser;
import ua.com.infinite.mobile.plains.NewsItem;
import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import android.text.TextUtils;

public class InfiniteContentProvider extends ContentProvider {

	private DBAdapter dbAdapter;

	private static final int USERS = 10;
	private static final int USER_ID = 11;
	private static final int NEWS = 20;
	private static final int NEWS_ID = 21;
	private static final int TRANSAKTIONS = 30;
	private static final int TRANSAKTION_ID = 31;

	public static final String AUTHORITY = "ua.com.infinite.mobile.contentprovider.InfiniteContentProvider";
	private static final String BASE_PATH = "mobile";

	public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/" + BASE_PATH);

	public static final Uri CONTENT_URI_USERS = TableUser.CONTENT_URI;
	public static final Uri CONTENT_URI_NEWS = TableNews.CONTENT_URI;
	public static final Uri CONTENT_URI_TRANSAKTIONS = TableTransaktions.CONTENT_URI;

	private static final UriMatcher IMUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
	static {
		IMUriMatcher.addURI(AUTHORITY, TableUser.TABLE_NAME, USERS);
		IMUriMatcher.addURI(AUTHORITY, TableUser.TABLE_NAME + "/#", USER_ID);
		IMUriMatcher.addURI(AUTHORITY, TableNews.TABLE_NAME, NEWS);
		IMUriMatcher.addURI(AUTHORITY, TableNews.TABLE_NAME + "/#", NEWS_ID);
		IMUriMatcher.addURI(AUTHORITY, TableTransaktions.TABLE_NAME, TRANSAKTIONS);
		IMUriMatcher.addURI(AUTHORITY, TableTransaktions.TABLE_NAME + "/#", TRANSAKTION_ID);
	}

	@Override
	public String getType(Uri uri) {
		return null;
	}

	@Override
	public boolean onCreate() {
		dbAdapter = new DBAdapter(getContext());
		return false;
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
		SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
		int uriType = IMUriMatcher.match(uri);
		switch (uriType) {
		case USERS:
			queryBuilder.setTables(TableUser.TABLE_NAME);
			break;
		case USER_ID:
			queryBuilder.setTables(TableUser.TABLE_NAME);
			queryBuilder.appendWhere(TableUser.COLUMN_ID + "=" + uri.getLastPathSegment());
			break;
		case NEWS:
			queryBuilder.setTables(TableNews.TABLE_NAME);
			break;
		case NEWS_ID:
			queryBuilder.setTables(TableNews.TABLE_NAME);
			queryBuilder.appendWhere(TableNews.COLUMN_ID + "=" + uri.getLastPathSegment());
			break;
		case TRANSAKTIONS:
			queryBuilder.setTables(TableTransaktions.TABLE_NAME);
			break;
		case TRANSAKTION_ID:
			queryBuilder.setTables(TableTransaktions.TABLE_NAME);
			queryBuilder.appendWhere(TableTransaktions.COLUMN_ID + "=" + uri.getLastPathSegment());
			break;
		default:
			throw new IllegalArgumentException("Unknown URI: " + uri);
		}
		SQLiteDatabase db = dbAdapter.getWritableDatabase();
		Cursor cursor = queryBuilder.query(db, projection, selection, selectionArgs, null, null, sortOrder);
		cursor.setNotificationUri(getContext().getContentResolver(), uri);
		return cursor;
	}

	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		int uriType = IMUriMatcher.match(uri);
		SQLiteDatabase db = dbAdapter.getWritableDatabase();
		int rowsDeleted = 0;
		String id = "";
		switch (uriType) {
		case USERS:
			rowsDeleted = db.delete(TableUser.TABLE_NAME, selection, selectionArgs);
			break;
		case USER_ID:
			id = uri.getLastPathSegment();
			if (TextUtils.isEmpty(selection)) {
				rowsDeleted = db.delete(TableUser.TABLE_NAME, TableUser.COLUMN_ID + "=" + id, null);
			} else {
				rowsDeleted = db.delete(TableUser.TABLE_NAME, TableUser.COLUMN_ID + "=" + id + " and " + selection, selectionArgs);
			}
			break;
		case NEWS:
			rowsDeleted = db.delete(TableNews.TABLE_NAME, selection, selectionArgs);
			break;
		case NEWS_ID:
			id = uri.getLastPathSegment();
			if (TextUtils.isEmpty(selection)) {
				rowsDeleted = db.delete(TableNews.TABLE_NAME, TableNews.COLUMN_ID + "=" + id, null);
			} else {
				rowsDeleted = db.delete(TableNews.TABLE_NAME, TableNews.COLUMN_ID + "=" + id + " and " + selection, selectionArgs);
			}
			break;
		case TRANSAKTIONS:
			rowsDeleted = db.delete(TableTransaktions.TABLE_NAME, selection, selectionArgs);
			break;
		case TRANSAKTION_ID:
			id = uri.getLastPathSegment();
			if (TextUtils.isEmpty(selection)) {
				rowsDeleted = db.delete(TableTransaktions.TABLE_NAME, TableTransaktions.COLUMN_ID + "=" + id, null);
			} else {
				rowsDeleted = db.delete(TableTransaktions.TABLE_NAME, TableTransaktions.COLUMN_ID + "=" + id + " and " + selection, selectionArgs);
			}
			break;
		default:
			throw new IllegalArgumentException("Unknown URI: " + uri);
		}
		getContext().getContentResolver().notifyChange(uri, null);
		return rowsDeleted;
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {
		SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();

		int uriType = IMUriMatcher.match(uri);
		SQLiteDatabase db = dbAdapter.getWritableDatabase();
		Uri resultUri = null;
		long id = 0;
		switch (uriType) {
		case USERS:
			id = db.insert(TableUser.TABLE_NAME, null, values);
			resultUri = ContentUris.withAppendedId(TableUser.CONTENT_URI, id);
			break;
		case NEWS:

			queryBuilder.setTables(TableNews.TABLE_NAME);
			String[] projectionForNews = new String[] { TableNews.COLUMN_ID };
			String selectionForNews = TableNews.COLUMN_ID + "=?";
			String[] selectionArgsForNews = new String[] { values.getAsString(TableNews.COLUMN_ID) };
			Cursor cursorForNews = queryBuilder.query(db, projectionForNews, selectionForNews, selectionArgsForNews, null, null, null);
			if (cursorForNews != null && cursorForNews.moveToFirst()) {
				db.update(TableNews.TABLE_NAME, values, selectionForNews, selectionArgsForNews);
				resultUri = ContentUris.withAppendedId(TableNews.CONTENT_URI,
						cursorForNews.getLong(cursorForNews.getColumnIndex(TableNews.COLUMN_ID)));
			} else {
				id = db.insert(TableNews.TABLE_NAME, null, values);
				resultUri = ContentUris.withAppendedId(TableNews.CONTENT_URI, id);
			}
			cursorForNews.close();
			break;
		case TRANSAKTIONS:
			id = db.insert(TableTransaktions.TABLE_NAME, null, values);
			resultUri = ContentUris.withAppendedId(TableTransaktions.CONTENT_URI, id);
			break;
		default:
			throw new IllegalArgumentException("Unknown URI: " + uri);
		}
		getContext().getContentResolver().notifyChange(uri, null);
		return resultUri;
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
		int uriType = IMUriMatcher.match(uri);
		SQLiteDatabase db = dbAdapter.getWritableDatabase();
		int rowsUpdated = 0;
		String id;
		switch (uriType) {
		case USERS:
			rowsUpdated = db.update(TableUser.TABLE_NAME, values, selection, selectionArgs);
			break;
		case USER_ID:
			id = uri.getLastPathSegment();
			if (TextUtils.isEmpty(selection)) {
				rowsUpdated = db.update(TableUser.TABLE_NAME, values, TableUser.COLUMN_ID + "=" + id, null);
			} else {
				rowsUpdated = db.update(TableUser.TABLE_NAME, values, TableUser.COLUMN_ID + "=" + id + " and " + selection, selectionArgs);
			}
			break;
		case NEWS:
			rowsUpdated = db.update(TableNews.TABLE_NAME, values, selection, selectionArgs);
			break;
		case NEWS_ID:
			id = uri.getLastPathSegment();
			if (TextUtils.isEmpty(selection)) {
				rowsUpdated = db.update(TableNews.TABLE_NAME, values, TableNews.COLUMN_ID + "=" + id, null);
			} else {
				rowsUpdated = db.update(TableNews.TABLE_NAME, values, TableNews.COLUMN_ID + "=" + id + " and " + selection, selectionArgs);
			}
			break;
		case TRANSAKTIONS:
			rowsUpdated = db.update(TableTransaktions.TABLE_NAME, values, selection, selectionArgs);
			break;
		case TRANSAKTION_ID:
			id = uri.getLastPathSegment();
			if (TextUtils.isEmpty(selection)) {
				rowsUpdated = db.update(TableTransaktions.TABLE_NAME, values, TableTransaktions.COLUMN_ID + "=" + id, null);
			} else {
				rowsUpdated = db.update(TableTransaktions.TABLE_NAME, values, TableTransaktions.COLUMN_ID + "=" + id + " and " + selection,
						selectionArgs);
			}
			break;
		default:
			throw new IllegalArgumentException("Unknown URI: " + uri);
		}
		getContext().getContentResolver().notifyChange(uri, null);
		return rowsUpdated;
	}

}
